package com.designMode.callback;

import java.util.*;
import java.util.stream.Collectors;

class HistoryResult {
    private int id;
    private int userId;
    private String operatorUserName;
    private int status;

    HistoryResult(int id, int userId, String operatorUserName, int status) {
        this.id = id;
        this.userId = userId;
        this.operatorUserName = operatorUserName;
        this.status = status;
    }

    public int getId() {
        return id;
    }

    public int getUserId() {
        return userId;
    }

    public String getOperatorUserName() {
        return operatorUserName;
    }

    public int getStatus() {
        return status;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public void setOperatorUserName(String operatorUserName) {
        this.operatorUserName = operatorUserName;
    }

    public void setStatus(int status) {
        this.status = status;
    }
}

public class Main {

    public static HashMap<Integer,Integer> userSet = new HashMap<>();

    public static List<HistoryResult> list1 = new ArrayList<>();
    public static Integer indexId = 1;
    /**
     * 查询处罚记录，记录按照id升序排序
     *
     * @param pageNo   第几页
     * @param pageSize 页数大小
     * @return 查询结果
     */
    public List<HistoryResult> query(int pageNo, int pageSize) {
        Collections.sort(list1, new Comparator<HistoryResult>() {
            @Override
            public int compare(HistoryResult o1, HistoryResult o2) {
                return o1.getId() - o2.getId();
            }
        });
        int fromIndex = (pageNo -1) * pageSize;
        int toIndex = pageNo * pageSize;
        return list1.stream().skip(fromIndex).limit(pageSize).collect(Collectors.toList());
    }

    /**
     * 查询某个用户的处罚记录，记录按照id升序排序
     *
     * @param userId 用户
     * @return 处罚记录
     */
    public List<HistoryResult> getByUserId(int userId) {
        List<HistoryResult> collect = list1.stream().filter(e -> e.getUserId() == userId).collect(Collectors.toList());
        Collections.sort(collect, new Comparator<HistoryResult>() {
            @Override
            public int compare(HistoryResult o1, HistoryResult o2) {
                return o1.getId() - o2.getId();
            }
        });
        return collect;
    }

    /**
     * 处罚操作, 如果用户已经有被处罚了，新的处罚不能低于当前处罚等级才能生效
     *
     * @param operatorUserName 操作者的名字
     * @param userId           处罚的用户
     * @param punishStatus     处罚类型
     * @return 返回处罚的记录id, 处罚不成功返回-1
     */
    public int punish(String operatorUserName, int userId, int punishStatus) {

        //包含用户id
       if (userSet.containsKey(userId)) {
           //新的处罚比他低，则无法生效
           if (userSet.get(userId) > punishStatus) {
               return  -1;
           }
           else {
               userSet.put(userId,punishStatus);
               for (int i = 0; i < list1.size(); i++) {
                   if (list1.get(i).getUserId()== userId) {
                       list1.get(i).setStatus(punishStatus);
                       list1.get(i).setId(indexId);
                       indexId ++;
                       return indexId - 1;

                   }
               }
           }

        }
       //不包含用户id
       else {
           userSet.put(userId,punishStatus);
           HistoryResult historyResult = new HistoryResult(indexId, userId, operatorUserName, punishStatus);
           indexId ++;
           list1.add(historyResult);
           return indexId - 1;
       }
       return -1;
    }

    /**
     * 解除当前处罚
     *
     * @param operatorUserName 操作者的名字
     * @param userId           解除处罚的用户
     * @return 如果当前用户正在被处罚中，解除当前处罚，返回处罚记录id，如果用户没有被处罚，返回-1表示解除处罚非法
     */
    public int relieve(String operatorUserName, int userId) {
        if (!userSet.containsKey(userId)) {
            return -1;
        }
        for (int i = 0; i < list1.size(); i++) {
            if (list1.get(i).getUserId() == userId) {
//                list1.get(i).setStatus(0);
                list1.add(new HistoryResult(indexId,userId,operatorUserName,0));
                indexId++;
                return indexId - 1;
            }

        }
        return 1;
    }

    public static void main(String[] args) throws Exception {
        Main main = new Main();
        Scanner in = new Scanner(System.in);
        List<HistoryResult> printHistory = new ArrayList<>();
        List<Integer> printOperators = new ArrayList<>();
        int opNum = Integer.parseInt(in.nextLine());
        while (in.hasNext()) {
            String op = in.nextLine();
            String data = in.nextLine();
            if (op.equals("punish")) {
                String[] punishData = data.split(" ");
                String operatorUserName = punishData[0];
                int userId = Integer.parseInt(punishData[1]);
                int punishStatus = Integer.parseInt(punishData[2]);
                printOperators.add(main.punish(operatorUserName, userId, punishStatus));
            } else if (op.equals("relieve")) {
                String[] relieveData = data.split(" ");
                String operatorUserName = relieveData[0];
                int userId = Integer.parseInt(relieveData[1]);
                printOperators.add(main.relieve(operatorUserName, userId));
            } else if (op.equals("getByUserId")) {
                int userId = Integer.parseInt(data);
                List<HistoryResult> results = main.getByUserId(userId);
                if (results != null && !results.isEmpty()) {
                    printHistory.addAll(results);
                }
            } else if (op.equals("query")) {
                String[] queryData = data.split(" ");
                int pageNo = Integer.parseInt(queryData[0]);
                int pageSize = Integer.parseInt(queryData[1]);
                List<HistoryResult> results = main.query(pageNo, pageSize);
                if (results != null && !results.isEmpty()) {
                    printHistory.addAll(results);
                }
            } else {
                throw new Exception("错误的输入");
            }
        }
        printHistory.forEach(res -> System.out.println(res.getId() + "_" + res.getUserId() + "_" + res.getOperatorUserName() + "_" + res.getStatus()));
        printOperators.forEach(System.out::println);
    }
}